package com.apache.attachment.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.attachment.entity.AttachmentRegister;
import com.apache.attachment.manager.AttachmentRegisterManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class AttachmentRegisterManagerImpl implements AttachmentRegisterManager {

	protected IDao attachmentRegisterDao;
	protected final String entityName = "com.apache.attachment.entity.AttachmentRegister";

	public void setAttachmentRegisterDao(IDao attachmentRegisterDao) {
		this.attachmentRegisterDao = attachmentRegisterDao;
	}

	public String saveInfo(ParamsVo<AttachmentRegister> vo) throws BusinessException {
		AttachmentRegister attachmentRegister = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="attachmentRegister_"+infoId;
		attachmentRegister.setRid(infoId);
		attachmentRegister.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		MethodParam param = new MethodParam("AttachmentRegister", cacheKey, "", entityName);
		param.setVaule(attachmentRegister);
		if (attachmentRegisterDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<AttachmentRegister> vo) throws BusinessException {
		AttachmentRegister attachmentRegister = vo.getObj();
		if (Validator.isNotNull(attachmentRegister.getRid())) {
			String cacheKey = "";
			//String cacheKey="attachmentRegister_"+attachmentRegister.getRid();
			MethodParam param = new MethodParam("AttachmentRegister", cacheKey, "", entityName);
			param.setVaule(attachmentRegister);
			return attachmentRegisterDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<AttachmentRegister> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="attachmentRegister_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		AttachmentRegister info = (AttachmentRegister) attachmentRegisterDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("AttachmentRegister");
			return attachmentRegisterDao.edit(param);
		} else {
			param.setParams("rid", infoId);
			param.setDelete(true);
			return attachmentRegisterDao.delete(param);
		}
	}

	/**
	 * description: 通过注册名验证唯一
	 * @param regEname
	 * @return  
	 * @author mhguo
	 * @update 2015-5-13
	 */
	private boolean checkUniqByREname(String regEname) {
		boolean mark = false;
		String sql = attachmentRegisterDao.getSql(3);
		MethodParam param = new MethodParam("ByProperty", "", sql, entityName);
		param.setParams("regEname", regEname);
		long count = attachmentRegisterDao.count(param);
		if (count > 0)
			return true;
		return mark;
	}

	private boolean checkUniqByREnameAndRid(String regEname, String rid) {
		boolean mark = false;
		String sql = attachmentRegisterDao.getSql(4);
		MethodParam param = new MethodParam("ByREnameAndRid", "", sql, entityName);
		param.setParams("regEname", regEname);
		param.setParams("rid", rid);//排除本身
		long count = attachmentRegisterDao.count(param);
		if (count > 0)
			return true;
		return mark;
	}

	public Object getInfoById(ParamsVo<AttachmentRegister> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="attachmentRegister_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return attachmentRegisterDao.selectById(param);
	}

	public Object execute(ParamsVo<AttachmentRegister> vo) {
		String methodKey = vo.getMethodKey();
		if ("checkUniqByREname".equalsIgnoreCase(methodKey)) {//通过注册名验证唯一
			String regEname = String.valueOf(vo.getParams("regEname")).trim();
			return checkUniqByREname(regEname);
		} else if ("checkUniqByREnameAndRid".equalsIgnoreCase(methodKey)) {
			String regEname = String.valueOf(vo.getParams("regEname")).trim();
			String rid = String.valueOf(vo.getParams("rid")).trim();
			return checkUniqByREnameAndRid(regEname, rid);
		}
		return null;
	}

	public Page getPageInfo(ParamsVo<AttachmentRegister> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = attachmentRegisterDao.pageSelect(param);
		return page;
	}

	public List<AttachmentRegister> getList(ParamsVo<AttachmentRegister> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return attachmentRegisterDao.select(param);
	}

	public long countInfo(ParamsVo<AttachmentRegister> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return attachmentRegisterDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<AttachmentRegister> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(attachmentRegisterDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		AttachmentRegister attachmentRegister = vo.getObj();

		if (null == attachmentRegister) {
			param.setSqlStr(sb.toString());
			return param;
		}

		if (Validator.isNotNull(attachmentRegister.getRegCname())) {
			sb.append(" and regCname = :regCname");
			param.setParams("regCname", attachmentRegister.getRegCname());
		}
		if (Validator.isNotNull(attachmentRegister.getRegEname())) {
			sb.append(" and regEname = :regEname");
			param.setParams("regEname", attachmentRegister.getRegEname());
		}
		if (Validator.isNotNull(attachmentRegister.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", attachmentRegister.getCreateTime());
		}
		if (Validator.isNotNull(attachmentRegister.getCreateSpaceEname())) {
			sb.append(" and createSpaceEname = :createSpaceEname");
			param.setParams("createSpaceEname", attachmentRegister.getCreateSpaceEname());
		}
		if (Validator.isNotNull(attachmentRegister.getCreateUserRealname())) {
			sb.append(" and createUserRealname = :createUserRealname");
			param.setParams("createUserRealname", attachmentRegister.getCreateUserRealname());
		}
		if (Validator.isNotNull(attachmentRegister.getCreateUserId())) {
			sb.append(" and createUserId = :createUserId");
			param.setParams("createUserId", attachmentRegister.getCreateUserId());
		}
		if (Validator.isNotNull(attachmentRegister.getAlloweTypes())) {
			sb.append(" and alloweTypes = :alloweTypes");
			param.setParams("alloweTypes", attachmentRegister.getAlloweTypes());
		}
		if (Validator.isNotNull(attachmentRegister.getWatermarkFile())) {
			sb.append(" and watermarkFile = :watermarkFile");
			param.setParams("watermarkFile", attachmentRegister.getWatermarkFile());
		}
		if (Validator.isNotNull(attachmentRegister.getWriteControlLevel())) {
			sb.append(" and writeControlLevel = :writeControlLevel");
			param.setParams("writeControlLevel", attachmentRegister.getWriteControlLevel());
		}
		if (Validator.isNotNull(attachmentRegister.getWriteKey())) {
			sb.append(" and writeKey = :writeKey");
			param.setParams("writeKey", attachmentRegister.getWriteKey());
		}
		if (Validator.isNotNull(attachmentRegister.getReadControlLevel())) {
			sb.append(" and readControlLevel = :readControlLevel");
			param.setParams("readControlLevel", attachmentRegister.getReadControlLevel());
		}
		if (Validator.isNotNull(attachmentRegister.getReadKey())) {
			sb.append(" and readKey = :readKey");
			param.setParams("readKey", attachmentRegister.getReadKey());
		}
		if (Validator.isNotNull(attachmentRegister.getIpAddress())) {
			sb.append(" and ipAddress = :ipAddress");
			param.setParams("ipAddress", attachmentRegister.getIpAddress());
		}
		if (Validator.isNotNull(attachmentRegister.getAttaSort())) {
			sb.append(" and attaSort = :attaSort");
			param.setParams("attaSort", attachmentRegister.getAttaSort());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
